package com.yuelao.thread.demo;

import org.joda.time.DateTime;

import java.util.concurrent.TimeUnit;

/**
 * 线程暂停、恢复、终止
 * Created by fengliye on 2017/10/5.
 */
public class ThreadDemo01 {
    static class Runner implements Runnable {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(100);
                    System.out.println(Thread.currentThread().getName() + " run at " +
                            new DateTime().toString("yyyy:MM:dd"));
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Thread run = new Thread(new Runner(), "runner");
        run.setDaemon(true);
        run.start();
        TimeUnit.SECONDS.sleep(3);
        // 将线程暂停
        run.suspend();
        System.out.println("suspend.....");
        // 将线程进行恢复，输出内容继续
        TimeUnit.SECONDS.sleep(3);
        run.resume();
        System.out.println("resume......");
        // 将线程终止
        TimeUnit.SECONDS.sleep(3);
        run.stop();
        System.out.println("stop.....");
    }
}
